package com.atguigu.lease.common.converter;


import com.atguigu.lease.model.enums.ItemType;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

/**
 * @Author: shenhao
 * @CreatTime: 2024/9/5 15:56
 * @Version: 1.0
 */
@Component
public class StringToItemTypeConverter implements Converter<String, ItemType> {
    @Override
    public ItemType convert(String value) {
        for (ItemType itemType : ItemType.values()) {
            if (itemType.getCode().equals(Integer.valueOf(value))){
                return itemType;
            }
        }
        // 抛出运行时异常可以,不要把异常类型写错
        throw new IllegalArgumentException("code非法");
    }
}
